home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #2 / Amiga Plus CD - 2004 - No. 02.iso / AmiSoft / Comm / tcp / JabberwockySRC.lha / Jabberwocky / src / transfer.c < prev    next >
Encoding:
C/C++ Source or Header  |  2003-04-28  |  10.2 KB  |  375 lines

  1. /*  Copyright (C) 2002 Tom Parker (tom@carrott.org),
  2.                        Matthias Münch (matthias@amigaworld.de)
  3.  
  4.     This program is free software; you can redistribute it and/or modify
  5.     it under the terms of the GNU General Public License as published by
  6.     the Free Software Foundation; either version 2 of the License, or
  7.     (at your option) any later version.
  8.  
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU General Public License
  15.     along with this program; if not, write to the Free Software
  16.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  17.  
  18.     In addition, as a special exception, Tom Parker and Matthias Münch give
  19.     permission to link the code of this program with a TCP stack of your 
  20.     choice, any official MUI libraries or classes and any custom MUI classes
  21.     that should be necessary for the operation of this program.  This 
  22.     exception also gives you permission to distribute linked combinations 
  23.     including this software with any of the before-mentioned libraries and 
  24.     classes.  You must obey the GNU General Public License in all respects for
  25.     all of the code used other than that provided by the before-mentioned 
  26.     libraries and classes.  As part of this exception you are obliged to 
  27.     follow the license terms of the before-mentioned libraries, this license
  28.     does not compel you to follow those terms, but if you do not then you may
  29.     not link with those libraries.  If you modify this file, you may extend
  30.     this exception to your version of the file, but you are not obligated to
  31.     do so.  If you do not wish to do so, delete this exception statement from
  32.     your version.
  33. */
  34. /*
  35. ** file transfer window
  36. */
  37.  
  38. #include "common.h"
  39.  
  40. #include <MUI/NListview_mcc.h>
  41. #include <libraries/asl.h>
  42. #include <proto/dos.h>
  43.  
  44. static Object *xf_list = NULL;
  45. static Object *xf_accbut, *xf_rejbut, *xf_clebut, *xf_clobut;
  46.  
  47. list *xfers = NULL;
  48.  
  49. static ULONG xfer_new(struct IClass *cl, Object *obj, struct opSet *msg);
  50. static void xfer_op(int op);
  51. static MUI_LIST_DISP_DECL(xfer_disp, struct xferdata *xf);
  52. static MUI_LIST_COMP_DECL(xfer_comp, struct xferdata *xf1, struct xferdata *xf2);
  53. static ULONG xfer_req_new(struct IClass *cl, Object *obj, struct opSet *msg);
  54. static void xfer_req_send(struct xferreqdata *data);
  55.  
  56.  
  57. MUI_DISPATCH(xfer_dispatch)
  58. {
  59.     switch(msg->MethodID)
  60.     {
  61.     case OM_NEW:
  62.         return xfer_new(cl,obj,(APTR)msg);
  63.  
  64.     case TRANSFER_OP:
  65.         xfer_op((int)MARG1);
  66.         return 0;
  67.  
  68.     }
  69.     return DoSuperMethodA(cl, obj, msg);
  70. }
  71.  
  72.  
  73. static ULONG xfer_new(struct IClass *cl, Object *obj, struct opSet *msg)
  74. {
  75.     static struct Hook dispHook = { {0,0}, &xfer_disp, NULL, NULL };
  76.     static struct Hook compHook = { {0,0}, &xfer_comp, NULL, NULL };
  77.  
  78.     obj = (Object *)DoSuperNew(cl,obj,
  79.         MUIA_Window_ID, MAKE_ID('X', 'F', 'E', 'R'),
  80.         MUIA_Window_Title, "File Transfers",
  81.         MUIA_HelpNode, "window-filetrans",
  82.         WindowContents, VGroup,
  83.             Child, NListviewObject,
  84.                 MUIA_NListview_NList, (ULONG) xf_list = NListObject,
  85.                     InputListFrame,
  86.                     MUIA_Font, MUIV_Font_Tiny,
  87.                     MUIA_NList_Title, TRUE,
  88.                     MUIA_NList_Format, "BAR, BAR, BAR,",
  89.                     MUIA_NList_DisplayHook, &dispHook,
  90.                     MUIA_NList_CompareHook, &compHook,
  91.                     MUIA_CycleChain, 1,
  92.                 End,
  93.             End,
  94.             Child, HGroup,
  95.                 Child, xf_accbut = mui_button("Accept"),
  96.                 Child, xf_rejbut = mui_button("Reject"),
  97.                 Child, xf_clebut = mui_button("Cleanup"),
  98.                 Child, xf_clobut = mui_button("Close"),
  99.             End,
  100.         End,
  101.         TAG_MORE, msg->ops_AttrList);
  102.  
  103.     if(!obj) return(0);
  104.     
  105.     // not implemented yet
  106.     set(xf_rejbut, MUIA_Disabled, TRUE);
  107.     set(xf_clebut, MUIA_Disabled, TRUE);
  108.  
  109.     DoMethod(xf_accbut, MUIM_Notify, MUIA_Pressed, FALSE, obj, 2, TRANSFER_OP, 0);
  110.     DoMethod(xf_rejbut, MUIM_Notify, MUIA_Pressed, FALSE, obj, 2, TRANSFER_OP, 1);
  111.     DoMethod(xf_clebut, MUIM_Notify, MUIA_Pressed, FALSE, obj, 2, TRANSFER_OP, 2);
  112.     DoMethod(xf_clobut, MUIM_Notify, MUIA_Pressed, FALSE, obj, 3, MUIM_Set, MUIA_Window_Open, FALSE);
  113.  
  114.     DoMethod(obj, MUIM_Notify, MUIA_Window_CloseRequest, TRUE, obj, 3, MUIM_Set, MUIA_Window_Open, FALSE);
  115.  
  116.     return (ULONG)obj;
  117. }
  118.  
  119.  
  120. MUI_LIST_DISP_STATIC(xfer_disp, struct xferdata *xf)
  121. {
  122.     static char buf[50];
  123.     char *t = "";
  124.  
  125.     if(xf)
  126.     {
  127.         switch(xf->type)
  128.         {
  129.         case XFER_SEND: t = "Send"; break;
  130.         case XFER_RECEIVE: t = "Receive"; break;
  131.         }
  132.         *array++ = t;
  133.         switch(xf->state)
  134.         {
  135.         case XFER_OFFERING:    t = "Offering";    break;
  136.         case XFER_OFFERED:     t = "Offered";     break; 
  137.         case XFER_CONNECTED:   t = "Connected";   break;
  138.         case XFER_CONNECTING:  t = "Connecting";  break;
  139.         case XFER_REQUESTING:  t = "Requesting";  break;
  140.         case XFER_SENDING:     t = "Sending";     break;
  141.         case XFER_RECEIVING:   t = "Receiving";   break;
  142.         case XFER_FINISHED:    t = "Finished";    break;
  143.         case XFER_REJECTED:    t = "Rejected";    break;
  144.         case XFER_TIMEOUT:     t = "Timeout";     break;
  145.         case XFER_ERROR:       t = "Error";       break;
  146.         default: t = "???";
  147.         }
  148.         *array++ = t;
  149.         *array++ = xf->name;
  150.         if(xf->size)
  151.             sprintf(buf, "\33r%ld / %ld (%d%%)", xf->cursize, xf->size, (xf->cursize * 100) / xf->size);
  152.         else
  153.             buf[0] = '\0';
  154.         *array = buf;
  155.     }
  156.     else
  157.     {
  158.         *array++ = "Operation";
  159.         *array++ = "State";
  160.         *array++ = "File";
  161.         *array = "Size";
  162.     }
  163.  
  164.     return 0;
  165. }
  166.  
  167.  
  168. MUI_LIST_COMP_STATIC(xfer_comp, struct xferdata *xf1, struct xferdata *xf2)
  169. {
  170.     if(xf1->state == XFER_SEND && xf2->state != XFER_SEND) return(1);
  171.     return(-1);
  172. }
  173.  
  174.  
  175. static void xfer_op(int op)
  176. {
  177.     struct xferdata *xf;
  178.  
  179.     DoMethod(xf_list, MUIM_NList_GetEntry, MUIV_NList_GetEntry_Active, &xf);
  180.     if(!xf) return;
  181.  
  182.     switch(op)
  183.     {
  184.     case 0:
  185.         if(xf->type != XFER_RECEIVE || xf->state != XFER_OFFERED) return;
  186.         xf->state = XFER_CONNECTING;
  187.         xfer_refresh(xf);
  188.         open_url(xf->name);
  189.         break;
  190.  
  191.     }
  192. }
  193.  
  194.  
  195. void xfer_packet(ikspak *pak)
  196. {
  197.     struct xferdata *xf;
  198.  
  199.     if(pak->subtype != IKS_TYPE_SET) return;
  200.  
  201.     xf = malloc(sizeof(struct xferdata));
  202.     memset(xf, 0, sizeof(struct xferdata));
  203.     xf->type = XFER_RECEIVE;
  204.     xf->state = XFER_OFFERED;
  205.     xf->name = iks_find_cdata(iks_find(pak->x, "query"), "url");
  206.     xf->pak = pak;
  207.  
  208.     list_append(&xfers, xf);
  209.     DoMethod(xf_list, MUIM_NList_InsertSingle, xf, MUIV_NList_Insert_Bottom);
  210.  
  211.     set(gui.xferwin, MUIA_Window_Open, TRUE);
  212. }
  213.  
  214.  
  215. int xfer_test(void)
  216. {
  217.     struct xferdata *xf;
  218.  
  219.     xf = malloc(sizeof(struct xferdata));
  220.     memset(xf, 0, sizeof(struct xferdata));
  221.     xf->file = "sys:Veriler/gfx/ehore.gif";
  222.     xf->name = FilePart(xf->file);
  223.     xf->type = XFER_SEND;
  224.     xf->state = XFER_OFFERING;
  225.     list_append(&xfers, xf);
  226.     DoMethod(xf_list, MUIM_NList_InsertSingle, xf, MUIV_NList_Insert_Bottom);
  227.  
  228.  
  229. http_up();
  230.     /* fix? */
  231. }
  232.  
  233.  
  234. void xfer_share(juser u)
  235. {
  236.     Object *win;
  237.  
  238.     win = NewObject(gui.xfer_req_mcc->mcc_Class, NULL, TRANSFER_TO, (ULONG) u, TAG_DONE);
  239.     if(win)
  240.     {
  241.         DoMethod(gui.app, OM_ADDMEMBER, win);
  242.         set(win, MUIA_Window_Open, TRUE);
  243.     }
  244. }
  245.  
  246.  
  247. void xfer_refresh(struct xferdata *xf)
  248. {
  249.     long pos = MUIV_NList_GetPos_Start;
  250.  
  251. /*    DoMethod(xf_list, MUIM_NList_Sort); */
  252.     if(xf)
  253.     {
  254.         DoMethod(xf_list, MUIM_NList_GetPos, xf, &pos);
  255.         if(pos != MUIV_NList_GetPos_End)
  256.             DoMethod(xf_list, MUIM_NList_Redraw, pos);
  257.     }
  258. }
  259.  
  260.  
  261. MUI_DISPATCH(xfer_req_dispatch)
  262. {
  263.     switch(msg->MethodID)
  264.     {
  265.     case OM_NEW:
  266.         return xfer_req_new(cl,obj,(APTR)msg);
  267.  
  268.     case TRANSFER_SEND:
  269.         xfer_req_send(INST_DATA(cl,obj));
  270.         DoMethod(gui.app, MUIM_Application_PushMethod, gui.list, 2, ROSTER_CLOSEME, obj);
  271.         return 0;
  272.  
  273.     }
  274.     return DoSuperMethodA(cl, obj, msg);
  275. }
  276.  
  277.  
  278. static ULONG xfer_req_new(struct IClass *cl, Object *obj, struct opSet *msg)
  279. {
  280.     struct xferreqdata *data;
  281.     juser u;
  282.     Object *filestr, *descstr, *totxt, *sendbut, *canbut;
  283.  
  284.     obj = (Object *)DoSuperNew(cl,obj,
  285.         MUIA_Window_Title, "Send File",
  286.         MUIA_HelpNode, "window-sendfile",
  287.         WindowContents, VGroup,
  288.             Child, ColGroup(2),
  289.                 Child, Label2(MSG_XFER_TO),
  290.                 Child, (ULONG) totxt = TextObject,
  291.                     TextFrame,
  292.                     MUIA_Background, MUII_TextBack,
  293.                 End,
  294.                 Child, Label2(MSG_XFER_FILE),
  295.                 Child, (ULONG) filestr = PopaslObject,
  296.                     MUIA_Popstring_String, StringObject,
  297.                         StringFrame,
  298.                         MUIA_String_MaxLen, 256,
  299.                         MUIA_CycleChain, 1,
  300.                     End,
  301.                     MUIA_Popstring_Button, PopButton(MUII_PopFile),
  302.                     MUIA_Popasl_Type, ASL_FileRequest,
  303.                 End,
  304.                 Child, Label2(MSG_XFER_DESC),
  305.                 Child, (ULONG) descstr = StringObject,
  306.                     StringFrame,
  307.                     MUIA_String_MaxLen, 256,
  308.                     MUIA_CycleChain, 1,
  309.                 End,
  310.             End,
  311.             Child, RectangleObject,
  312.                 MUIA_FixHeightTxt, "M",
  313.                 MUIA_Rectangle_HBar, TRUE,
  314.             End,
  315.             Child, HGroup,
  316.                 Child, sendbut = mui_button(MSG_XFER_SEND_GAD),
  317.                 Child, canbut = mui_button(MSG_XFER_CANCEL_GAD),
  318.             End,
  319.         End,
  320.         TAG_MORE, msg->ops_AttrList);
  321.  
  322.     if(!obj) return(0);
  323.  
  324.     u = (juser)GetTagData(TRANSFER_TO, NULL, msg->ops_AttrList);
  325.     if(u->name)
  326.         set(totxt, MUIA_Text_Contents, (ULONG) my_printf("%s (%s)", u->name, iks_id_print(u->id)));
  327.     else
  328.         set(totxt, MUIA_Text_Contents, (ULONG) iks_id_print(u->id));
  329.  
  330.     data = INST_DATA(cl,obj);
  331.     data->filestr = filestr;
  332.     data->descstr = descstr;
  333.     data->jid = iks_strdup(iks_id_print(u->id));
  334.  
  335.     DoMethod(sendbut, MUIM_Notify, MUIA_Pressed, FALSE, obj, 1, TRANSFER_SEND);
  336.  
  337.     DoMethod(canbut, MUIM_Notify, MUIA_Pressed, FALSE, MUIV_Notify_Application, 5, MUIM_Application_PushMethod, gui.list, 2, ROSTER_CLOSEME, obj);
  338.     DoMethod(obj, MUIM_Notify, MUIA_Window_CloseRequest, TRUE, MUIV_Notify_Application, 5, MUIM_Application_PushMethod, gui.list, 2, ROSTER_CLOSEME, obj);
  339.  
  340.     return((ULONG)obj);
  341. }
  342.  
  343.  
  344. static void xfer_req_send(struct xferreqdata *data)
  345. {
  346.     struct xferdata *xf;
  347.     iks *x, *y;
  348.     char *file, *desc;
  349.  
  350.     http_up();
  351.  
  352.     file = mui_sget(data->filestr);
  353.     desc = mui_sget(data->descstr);
  354.     if(!file) return;
  355.     x = iks_make_iq(IKS_TYPE_SET, IKS_NS_OOB);
  356.     iks_insert_attrib(x, "to", data->jid);
  357.     y = iks_find(x, "query");
  358.  
  359.     xf = malloc(sizeof(struct xferdata));
  360.     memset(xf, 0, sizeof(struct xferdata));
  361.     xf->file = strdup(file);
  362.     xf->name = FilePart(xf->file);
  363.     xf->type = XFER_SEND;
  364.     xf->state = XFER_OFFERING;
  365.     list_append(&xfers, xf);
  366.     DoMethod(xf_list, MUIM_NList_InsertSingle, xf, MUIV_NList_Insert_Bottom);
  367.  
  368.     /* FIX ME */
  369.     iks_insert_cdata(iks_insert(y, "url"), my_printf("http://%s/%s", net_myip(), xf->name) , -1);
  370.     if(desc) iks_insert_cdata(iks_insert(y, "desc"), desc, -1);
  371.  
  372.     iks_send(net.parser, x);
  373.     iks_delete(x);
  374. }
  375.